路由
文档:.NET MAUI Shell 概述 教程:使用选项卡和浮出控件导航创建多页 .NET MAUI 应用
注册路由
可以在 FlyoutItem、TabBar、Tab 和 ShellContent 对象上通过其 Route 属性定义路线:
<Shell ...>
<FlyoutItem ...
Route = "astronomy">
<ShellContent ...
Route="moonphase" />
<ShellContent ...
Route="sunrise" />
</FlyoutItem>
<FlyoutItem>
<ShellContent ...
Route="about" />
</FlyoutItem>
</Shell>
若要导航到 moonphase 路线,绝对路线 URI 为 //astronomy/moonphase
注册详细路线
在 Shell 子类构造函数中或者在调用路线之前运行的任何其他位置,可为 Shell 可视化层次结构中未显示的任何详细信息页面使用 Routing.RegisterRoute 来显式注册路线。
Routing.RegisterRoute("astronomicalbodydetails", typeof(AstronomicalBodyPage));
若要导航到 AstronomicalBodyPage,请调用:
await Shell.Current.GoToAsync("astronomicalbodydetails");`
向后导航
向后导航可以通过将“..”指定为 GoToAsync 方法的参数来执行:
await Shell.Current.GoToAsync("..");
传递数据
执行基于 URI 的编程导航时,可将基元数据作为基于字符串的查询参数传递。 若要传递数据,请在路线后面追加 ?,后接查询参数 ID、= 和一个值:
string celestialName = "moon";
await Shell.Current.GoToAsync($"astronomicalbodydetails?bodyName={celestialName}");
在此示例中,路线为 astronomicalbodydetails,参数为 bodyName,值来自变量 。
检索数据(接收数据)
对于每个基于字符串的查询参数和基于对象的导航参数,可以通过使用 QueryPropertyAttribute 修饰接收类来接收导航数据:
[QueryProperty(nameof(AstroName), "bodyName")]
public partial class AstronomicalBodyPage : ContentPage
{
string astroName;
public string AstroName
{
get => astroName;
set
{
astroName = value;
}
}
...
}
在此示例中,QueryPropertyAttribute 的第一个参数指定将要接收数据的属性的名称,第二个参数指定参数 ID。